package com.springcloudalibaba.gateway.filter.gateway;

import com.springcloudalibaba.bclass.base.BaseResult;
import com.springcloudalibaba.bclass.util.LoggerUtil;
import com.springcloudalibaba.gateway.enums.GatewayFilterOrderEnum;
import com.springcloudalibaba.gateway.properties.CustomProperties;
import com.springcloudalibaba.gateway.util.MonoResponseUtil;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @描述: TODO IP网关过滤器，不需要在配置文件中配置，作用于所有的请求
 * @作者: lixing
 * @日期 2021/6/24 10:25
 */
public class MyIpGatewayFilter implements GatewayFilter, Ordered {
    /**
     * 通过构造函数初始化参数配置类
     */
    private CustomProperties customProperties;
    private MyIpGatewayFilterFactoryConfig myIpGatewayFilterFactoryConfig;
    MyIpGatewayFilter(CustomProperties customProperties, MyIpGatewayFilterFactoryConfig myIpGatewayFilterFactoryConfig) {
        this.customProperties = customProperties;
        this.myIpGatewayFilterFactoryConfig = myIpGatewayFilterFactoryConfig;
    }

    /**
     * 过滤器执行的顺序，值越小，执行顺序越靠前
     */
    @Override
    public int getOrder() {
        return GatewayFilterOrderEnum.MY_IP.getValue();
    }

    /**
     * 过滤
     */
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        if ("*".equalsIgnoreCase(myIpGatewayFilterFactoryConfig.getWhiteIpStr())) {
            return chain.filter(exchange);
        } else {
            ServerHttpRequest serverHttpRequest = exchange.getRequest();
            ServerHttpResponse serverHttpResponse = exchange.getResponse();
            // 请求地址
            String reqPath = serverHttpRequest.getURI().getPath();
            // 当前请求的ip
            String ip = Objects.requireNonNull(exchange.getRequest().getRemoteAddress()).getAddress().getHostAddress();
            // 公共白名单列表
            List<String> ignorePropertieIpList = Arrays.asList(customProperties.getIgnore().getIp().split(","));
            // 局部白名单列表
            List<String> configIpList = Arrays.asList(myIpGatewayFilterFactoryConfig.getWhiteIpStr().split(","));
            //
            LoggerUtil.info("MyIpGatewayFilter 网关过滤器 ---- start");
            LoggerUtil.info(MessageFormat.format("当前ip: {0}", ip));
            LoggerUtil.info("MyIpGatewayFilter 网关过滤器 ---- end");
            LoggerUtil.info("");
            //
            if (ignorePropertieIpList.contains(ip) || configIpList.contains(ip)) {
                return chain.filter(exchange);
            }
            return MonoResponseUtil.getVoidMono(serverHttpResponse, new BaseResult<String>().fail("您还没有权限访问，请联系管理员"));
        }
    }

}
